package com.chushouya.manager.dao.repository;

import com.general.framework.core.lang.Dates;
import com.general.framework.core.lang.Lists;
import com.general.framework.core.lang.Longs;
import com.general.framework.core.lang.Numbers;
import com.general.framework.core.schema.PageRequest;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.manager.constants.BonusStatusEnum;
import com.chushouya.manager.dao.entity.BonusUserEntity;
import com.chushouya.manager.dao.mapper.BonusUserEntityMapper;
import com.chushouya.manager.dto.admin.bonus.BonusQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

@Repository
@Slf4j
public class BonusUserRepository extends BaseRepository<BonusUserEntityMapper, BonusUserEntity, Long> {

    public boolean existsWithUserIdAndBonusTypeIds(Long userId, List<Long> bonusTypeIds) {
        if (userId == null || Lists.isEmpty(bonusTypeIds)) {
            return false;
        }
        Example example = buildExample();
        example.and().andEqualTo("userId", userId).andIn("typeId", bonusTypeIds);
        return selectCountByExample(example) > 0;
    }

//    public List<BonusTypeExportDTO> selectTicketNo(Long bonusTypeId) {
//
//        return mapper.selectTicketNo(bonusTypeId);
//    }

    public BonusUserEntity selectFirstByTicketNo(String ticketNo) {
        Example example = buildExample();
        example.and().andEqualTo("ticketNo", ticketNo);
        return selectFirstOneByExample(example);
    }

    public PageResult<BonusUserEntity> selectPageByBonusTypeId(Long bonusTypeId, PageRequest pageRequest) {
        if (bonusTypeId == null) {
            return PageResult.empty();
        }
        Example example = buildExample();
        example.and().andEqualTo("typeId", bonusTypeId);
        example.orderBy("bonusId").desc();
        return selectPage(pageRequest, example);
    }

    public Boolean existsWithBonusTypeId(Long bonusTypeId) {
        if (bonusTypeId == null) {
            return false;
        }
        Example example = buildExample();
        example.and().andEqualTo("typeId", bonusTypeId);
        return selectCountByExample(example) > 0;
    }

    public List<BonusUserEntity> selectList(BonusQuery query) {
        final Example example = this.buildExample(query);
        example.setOrderByClause("type_money+1 desc");
        return selectByExample(example);
    }

    public BonusUserEntity selectOne(BonusQuery query) {
        final Example example = this.buildExample(query);
        return selectFirstOneByExample(example);
    }

    private Example buildExample(BonusQuery query) {
        Example example = this.buildExample();
        if (Objects.nonNull(query.getAppId())) {
            example.and().andEqualTo("appId", query.getAppId());
        }
        if (Objects.equals(query.getBonusStatus(), BonusStatusEnum.UNUSED.value())){
            example.and().andEqualTo("orderId", Numbers.INTEGER_ZERO);
            example.and().andGreaterThan("endTime", Dates.getTimeNow());
        } else if (Objects.equals(query.getBonusStatus(), BonusStatusEnum.USED.value())){
            example.and().andNotEqualTo("orderId", Numbers.INTEGER_ZERO);
        } else if (Objects.equals(query.getBonusStatus(), BonusStatusEnum.EXPIRED.value())){
            example.and().andEqualTo("orderId", Numbers.INTEGER_ZERO);
            example.and().andLessThan("endTime", Dates.getTimeNow());
        }

        if (Objects.nonNull(query.getTypeId())) {
            example.and().andEqualTo("typeId", query.getTypeId());
        }

        if(Longs.notNullAndZero(query.getProductId())){

        }

        if(Longs.notNullAndZero(query.getCategoryId())){

        }

        if (Longs.notNullAndZero(query.getUserId())) {
            example.and().andEqualTo("userId", query.getUserId());
        }

        if (Longs.notNullAndZero(query.getBonusId())) {
            example.and().andEqualTo("bonusId", query.getBonusId());
        }

        if (Objects.nonNull(query.getBonusType())) {
            example.and().andEqualTo("bonusType", query.getBonusType());
        }

        if(Objects.nonNull(query.getAmount())){
            example.and().andLessThan("minAmount", query.getAmount());
        }

        return example;
    }
}
